<template>
    <el-card class="box-card" style="margin: 10px 0;">
        <div slot="header" class="clearfix">
            <el-tabs class="tab" v-model="activeName">
                <el-tab-pane label="销售额" name="sale"></el-tab-pane>
                <el-tab-pane label="访问量" name="visit"></el-tab-pane>
            </el-tabs>

            <div class="right">
                <span @click="setDay">今日</span>
                <span @click="setWeek">本周</span>
                <span @click="setMonth">本月</span>
                <span @click="setYear">本年</span>
                <el-date-picker v-model="date" value-format="yyyy-MM-dd" type="daterange" start-placeholder="开始日期"
                    end-placeholder="结束日期" :default-time="['12:00:00']" class="date" size="mini">
                </el-date-picker>
            </div>
        </div>
        <div>
            <el-row :gutter="10">
                <el-col :span="18">
                    <div class="charts" ref="charts"></div>
                </el-col>
                <el-col :span="6">
                    <div class="contentRight">
                        <h3>门店{{ title }}排名</h3>
                        <ul>
                            <li>
                                <span class="rindex index">1</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li>
                                <span class="rindex index">2</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li><span class="rindex index">3</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li><span class="index">4</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li><span class="index">5</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li><span class="index">6</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                            <li><span class="index">7</span>
                                <span class="rname">肯德基</span>
                                <span class="rvalue">123</span>
                            </li>
                        </ul>
                    </div>

                </el-col>
            </el-row>
        </div>
    </el-card>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'

import { mapState } from 'vuex';
export default {
    name: 'Sale',
    data() {
        return {
            activeName: 'sale',
            mycharts: null,
            // 收集日历的数组
            date: [],
        }
    },
    mounted() {
        this.mycharts = echarts.init(this.$refs.charts)
        this.mycharts.setOption({
            title: {
                text: '销售额趋势'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: [],
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: 'Direct',
                    type: 'bar',
                    barWidth: '60%',
                    data: []
                }
            ]
        })
    },
    computed: {
        title() {
            return this.activeName == 'sale' ? '销售额' : '访问量'
        },
        ...mapState({
            listState: state => state.home.list
        })
    },
    watch: {
        title() {
            // 重新修改图标配置的数据
            this.showData()
        },
        listState() {
            this.showData()
        }
    },
    methods: {
        showData() {
            this.mycharts.setOption({
                title: {
                    text: this.title
                },
                xAxis: {
                    data: this.title == '销售额' ? this.listState.orderFullYearAxis : this.listState.userFullYearAxis
                },
                series: {
                    data: this.title == '销售额' ? this.listState.orderFullYear : this.listState.userFullYear
                }
            })
        },
        setDay() {
            const day = dayjs().format('YYYY-MM-DD')
            this.date = [day, day]
        },
        setWeek() {
            const start = dayjs().day(1).format('YYYY-MM-DD')
            const end = dayjs().day(7).format('YYYY-MM-DD')
            this.date = [start, end]
        },
        setMonth() {
            const start = dayjs().startOf('month').format('YYYY-MM-DD')
            const end = dayjs().endOf('month').format('YYYY-MM-DD')
            this.date = [start, end]
        },
        setYear() {
            const start = dayjs().startOf('year').format('YYYY-MM-DD')
            const end = dayjs().endOf('year').format('YYYY-MM-DD')
            this.date = [start, end]
        }
    }
}
</script>

<style>
.el-card__header {
    border: 0
}
</style>

<style scoped>
.clearfix {
    position: relative;
    display: flex;
    justify-content: space-between;
}

.tab {
    width: 100%;
}

.right {
    position: absolute;
    right: 0;
}

.date {
    width: 300px;
    margin: 0 20px;
}

.right span {
    margin: 0 10px;
    font-size: 14px;
}

.charts {
    width: 100%;
    height: 300px;
}

.contentRight {
    position: relative;
    height: 300px;
}

.contentRight h3 {
    position: absolute;
    top: -30px;
    font-size: 14px;
}

ul {
    position: absolute;
    list-style: none;
    width: 100%;
    height: 300px;
    padding: 0;
}

ul li {
    height: 8%;
    margin: 10px 0;
    font-size: 12px;
}

.rindex {
    border-radius: 50%;
    background-color: black;
    color: white;

}

.index {
    float: left;
    width: 15px;
    height: 15px;
    line-height: 15px;
    text-align: center;
}

.rvalue {
    float: right;
}

.rname {
    margin-left: 15px;
}
</style>